// 获取全局应用程序实例对象
const app = getApp()

Page({
  data: {
    lists: [],
    orderid: ''
  },
  //加载获取页面数据
  onload(id) {
    var that = this;
    var params = {
      openid: app.globalData.info.openid,
      id: id
    };
    that.setData({
      id: id
    })
    app.network.POST(
      'https://www.ausck.com/api/my_order',
      {
        params: params,
        success: function (res) {
          //拿到解密后的数据，进行代码逻辑
          console.log(res.data.data)
          if (res.data.data[0]) {
            that.setData({
              lists: res.data.data
            })
          } else {
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/index/index'
              })
            }, 2000)
          }
        },
        fail: function () {
          //失败后的逻辑  
        },
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onload(options.id)
  },

  toPersonalInfoTap: function () {
    wx.navigateTo({
      url: '/pages/personal-information/personal-information'
    })
  },


  //待评价模态框
  powerDrawer: function (e) {
    if (e.currentTarget.dataset.orderid) {
      this.setData({
        orderid: e.currentTarget.dataset.orderid
      });
    }
    if (e.currentTarget.dataset.orderstatus) {
      this.setData({
        orderstatus: e.currentTarget.dataset.orderstatus
      });
    }
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  formSubmit: function (e) {
    var that = this;
    var params = {
      openid: app.globalData.info.openid,
      orderid: that.data.orderid,
      content: e.detail.value.content
    };
    app.network.POST(
      'https://www.ausck.com/api/evaluate',
      {
        params: params,
        success: function (res) {
          //拿到解密后的数据，进行代码逻辑
          if (res.data.status == 200) {
            that.onload(that.data.id)
          }
        },
        fail: function () {
          //失败后的逻辑  
        },
      })
  },

  //发收单模态框
  orderDrawer: function (e) {
    console.log(e.currentTarget.dataset.orderid)
    if (e.currentTarget.dataset.orderid) {
      this.setData({
        orderid: e.currentTarget.dataset.orderid
      });
    }
    if (e.currentTarget.dataset.orderstatus) {
      this.setData({
        orderstatus: e.currentTarget.dataset.orderstatus
      });
      var that = this;
      var params = {
        openid: app.globalData.info.openid,
        orderid: e.currentTarget.dataset.orderid,
        id: e.currentTarget.dataset.orderstatus
      };
      app.network.POST(
        'https://www.ausck.com/api/my_order',
        {
          params: params,
          success: function (res) {
            that.setData({
              service_time: res.data.data[0].service_time,
              project_title: res.data.data[0].project_title,
              total: res.data.data[0].total,
              car_info: res.data.data[0].plate_number + '/' + res.data.data[0].brand + '/' + res.data.data[0].color,
              address: res.data.data[0].address
            })
          },
          fail: function () {
            //失败后的逻辑  
          },
        })
    }
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu)
  },
  toServiceTap: function () {
    wx.navigateTo({
      url: '/pages/customer-service/customer-service?index=0'
    })
  },
  submitDrawer: function () {
    var that = this;
    var params = {
      openid: app.globalData.info.openid,
      orderid: that.data.orderid
    };
    app.network.POST(
      'https://www.ausck.com/api/change_status',
      {
        params: params,
        success: function (res) {
          if (res.data.status == 200) {
            that.util('close');
            that.onload(that.data.id);
          }
        },
        fail: function () {
          //失败后的逻辑  
        },
      })
  },



  //模态框动画
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示  
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  }

})